package cn.mw.cmdb.param;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.mw.cmdb.entity.AlgorithmInfo;
import cn.mw.cmdb.entity.InventoryWorkInfo;
import cn.mw.cmdb.service.ServiceChoose;
import cn.mw.cmdb.service.impl.InventoryWorkInfoServiceImpl;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;


/**
 * @Author shenwenyi
 * @Date 2024/4/26 15:54
 * @PackageName:cn.mw.cmdb.param
 * @ClassName: InventoryParam
 * @Description: TODD
 * @Version 1.0
 */
@Data
public class InventorySearchParam extends SearchParam implements ServiceChoose,SearchParamConvert{
    @Schema(description = "id")
    private String id;

    @Schema(description = "盘点任务名称")
    private String workName;

    @Schema(description = "盘点方式")
    private String  inventoryType;

    @Schema(description = "任务状态")
    private String workState;

    @Schema(description = "负责人")
    private String principal;

    @Schema(description = "盘点结果")
    private String inventoryResult;

    private String sortName;

    // ASC | DESC
    private String sortOrder;


    @Override
    public String getServiceId() {
        return InventoryWorkInfoServiceImpl.ID;
    }

    @Override
    public void convert() {
        //盘点任务名称
        String workName = getWorkName();
        if (StringUtils.hasText(workName)) {
            this.addSubCondition(CMDBTool.getFieldName(InventoryWorkInfo::getWorkName)
                    , new SubCondition(CriteriaOpsType.like.name(), workName));
        }
        //盘点方式
        String inventoryType = getInventoryType();
        if (StringUtils.hasText(inventoryType)) {
            this.addSubCondition(CMDBTool.getFieldName(InventoryWorkInfo::getInventoryType)
                    , new SubCondition(CriteriaOpsType.is.name(), workName));
        }
        //任务状态
        String workState = getWorkState();
        if (StringUtils.hasText(workState)) {
            this.addSubCondition(CMDBTool.getFieldName(InventoryWorkInfo::getWorkState)
                    , new SubCondition(CriteriaOpsType.is.name(), getWorkState()));
        }
        //盘点结果
        String inventoryResult = getInventoryResult();
        if (StringUtils.hasText(inventoryResult)) {
            this.addSubCondition(CMDBTool.getFieldName(InventoryWorkInfo::getInventoryResult)
                    , new SubCondition(CriteriaOpsType.is.name(), inventoryResult));
        }

        //负责人
        String userId = getPrincipal();
        if (ObjectUtil.isNotEmpty(userId)) {
            this.addSubCondition(CMDBTool.getFieldName(InventoryWorkInfo::getPrincipal)
                    , new SubCondition(CriteriaOpsType.in.name(), userId));
        }
    }

    public Sort getSort() {
        //Sort by = Sort.by(Sort.Direction.DESC, CMDBTool.getFieldName(AlgorithmInfo::getCreateTime));
        if (StrUtil.isBlank(sortName)) {
            String fieldName = CMDBTool.getFieldName(InventoryWorkInfo::getCreateDate);
            return Sort.by(Sort.Direction.DESC, fieldName);
        }
        return Sort.by(sortOrder, sortName);
    }

    public void sort(){
        if(StrUtil.isBlank(sortName)){
            setSortName(CMDBTool.getFieldName(InventoryWorkInfo::getCreateDate));
            setSortOrder("DESC");
        }
    }
}
